package pers.qianyu.month_202012.date_20201211;

import org.junit.*;

import java.util.*;

/**
 * 216. 组合总和 III
 * https://leetcode-cn.com/problems/combination-sum-iii/
 *
 * @author mizzle rain
 * @date 2020-12-11 17:12
 */
public class CombinationSumThree {
    List<List<Integer>> res = new ArrayList<>();

    public List<List<Integer>> combinationSum3(int k, int n) {
        ArrayList<Integer> path = new ArrayList<>();
        int startNum = 1;
        backTracking(path, k, n, startNum);
        return res;
    }

    private void backTracking(ArrayList<Integer> path, int k, int n, int startNum) {
        if (k == path.size() && n == 0) {
            res.add(new ArrayList<>(path));
            return;
        } else if (n < 0) {
            return;
        }
        for (int i = startNum; i <= 9; i++) {
            path.add(i);
            backTracking(path, k, n - i, i + 1);
            path.remove(path.size() - 1);
        }
    }

    @Test
    public void test1() {
        int k = 3, n = 9;
        List<List<Integer>> result = new CombinationSumThree().combinationSum3(k, n);
        result.forEach(System.out::println);
    }
}
